﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace ProjectBuilder
{
    [DbTable("phpdoc_file")]
    public class File : BaseDocObject
    {
        [DbField("name")]
        public string Name { get; set; }

        [DbField("source", IsUniqField = false)]
        public string Source { get; set; }

        public Folder Folder { get; set; }

        [DbField("folder_id")]
        public long? FolderId
        {
            get { return Folder.GetDBId(); }
        }

        public static long? getBDIdByPath(string path)
        {
            var folder = new Folder(path);
            var parts = path.Split('/');
            string fileName = parts.Last<string>();

            var cmd = new MySqlCommand();
            cmd.CommandText = "SELECT id FROM `phpdoc_file` WHERE `name` = '" + fileName + "' AND `folder_id` = '" + folder.GetDBId() + "'";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = Globals.Connection;
            var res = cmd.ExecuteScalar();
            if (res != null)
                return long.Parse(cmd.ExecuteScalar().ToString());
            throw new Exception("File with path " + path + " not found in DB");
        }
    }
}
